package com.infore.photobrowseselect;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.infore.photobrowseselect.matisse.internal.ui.widget.MediaGridInset;
import com.infore.photobrowseselect.transferee.style.index.NumberIndexIndicator;
import com.infore.photobrowseselect.transferee.style.progress.ProgressBarIndicator;
import com.infore.photobrowseselect.transferee.transfer.TransferConfig;
import com.infore.photobrowseselect.transferee.transfer.Transferee;

import java.util.ArrayList;

/**
 * 浏览图片 Fragment
 * 
 * @author dingxinlin
 * @date 2018-4-20
 */
public class BrowserPhotoFragment extends Fragment {

    public static final String EXTRA_PHOTO_URI = "EXTRA_PHOTO_URI";
    public static final String EXTRA_SPAN_COUNT = "EXTRA_SPAN_COUNT";

    private RecyclerView mRecyclerView;

    private int spanCount = 3;
    private ArrayList<String> uriList = new ArrayList<>();
    private BrowserPhotoAdapter mAdapter;

    protected Transferee transferee;
    protected TransferConfig config;


    public static BrowserPhotoFragment newInstance(ArrayList<String> photoUri, int spanCount) {
        BrowserPhotoFragment fragment = new BrowserPhotoFragment();
        Bundle args = new Bundle();
        args.putStringArrayList(EXTRA_PHOTO_URI, photoUri);
        args.putInt(EXTRA_SPAN_COUNT, spanCount);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_browser_photo, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRecyclerView = view.findViewById(R.id.recycler_view);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // 接收外部传入参数
        Bundle bundle = getArguments();
        ArrayList<String> uris = bundle.getStringArrayList(EXTRA_PHOTO_URI);
        if (uris == null || uris.size() == 0) {
            return;
        }
        uriList.addAll(uris);

        int extraSpanCount = bundle.getInt(EXTRA_SPAN_COUNT);
        if (extraSpanCount > 0) {
            this.spanCount = extraSpanCount;
        }

        initRecyclerView();

        initBrowserEngine();
    }

    /**
     * 初始化RecyclerView
     */
    private void initRecyclerView() {
        mAdapter = new BrowserPhotoAdapter();

        mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), spanCount));
        int spacing = getResources().getDimensionPixelSize(R.dimen.photo_grid_spacing);
        mRecyclerView.addItemDecoration(new MediaGridInset(spanCount, spacing, false));
        mRecyclerView.setAdapter(mAdapter);
    }

    /**
     * 初始化浏览引擎
     */
    private void initBrowserEngine() {
        config = TransferConfig.build()
                .setSourceImageList(uriList)
                .setMissPlaceHolder(R.drawable.image_loading)
                .setErrorPlaceHolder(R.drawable.image_fail)
                .setProgressIndicator(new ProgressBarIndicator())
                .setIndexIndicator(new NumberIndexIndicator())
                .setJustLoadHitImage(true)
                .setRecyclerView(mRecyclerView)
                .setImageId(R.id.image_view)
                .create();
        transferee = Transferee.getDefault(getContext());
    }

    /**
     * 适配器
     */
    private class BrowserPhotoAdapter extends RecyclerView.Adapter<BrowserPhotoAdapter.ViewHolder> {

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_browser_photo, parent, false);
            ViewHolder vh = new ViewHolder(view);
            return vh;
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, final int position) {
            holder.delete.setVisibility(View.GONE);
            Glide.with(holder.imageView.getContext())
                    .load(uriList.get(position))
                    .placeholder(R.drawable.image_loading)
                    .error(R.drawable.image_fail)
                    .centerCrop()
                    .into(holder.imageView);

            /**
             * item点击事件
             */
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // 大图浏览
                    config.setNowThumbnailIndex(position);
                    transferee.apply(config).show();
                }
            });
        }

        @Override
        public int getItemCount() {
            return uriList == null ? 0 : uriList.size();
        }

        /**
         * View Holder
         */
        class ViewHolder extends RecyclerView.ViewHolder {
            ImageView imageView;
            ImageView delete;

            public ViewHolder(View itemView) {
                super(itemView);
                imageView = itemView.findViewById(R.id.image_view);
                delete = itemView.findViewById(R.id.delete_btn);
            }
        }
    }
}
